package cn.edu.zhku.citrusmonitor;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.Message;

/*	disease: 11,				   // 病害信息
	pest: 22,		 			  // 虫害信息
	citrus: 33,		 			 // 柑橘种类
	plantTechnology: 44,        // 种植方法
	fertilizerTechnology: 55,  // 施肥技术
	medicineTechnology: 66,	  // 施药技术
	law: 77,	             // 政策法规
	question: 88,			// 问题
	防虫技术：101
	病害技术：102
 *	announcement 99;		   // 公告
 */
@WebServlet("/distri.do")
public class Distribution extends BaseServlet{
	/**
	 * 为了message_ul的转发，需要ci，可选currentPage，pageSize
	 * @param request
	 * @param response
	 * @return
	 */
	public String branchUl(HttpServletRequest request,HttpServletResponse response) {
		String ci=request.getParameter("ci");
		String currentPage=request.getParameter("currentPage");
		String pageSize=request.getParameter("pageSize");
		String message="";
		String url="";
		if(ci==null||ci.equals(""))
			return JSON.toJSONString(new Message("2","ci不能为空"));
		else {
			switch(ci) {
				case "11":
					url="/citrusmonitor/disease.do?method=findAll";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "22":
					url="/citrusmonitor/pest.do?method=findAll";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "33":
					url="/citrusmonitor/citrus.do?method=getCitrusPageList";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "44":
					url="/citrusmonitor/technology.do?method=findPlant";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "55":
					url="/citrusmonitor/technology.do?method=findFertilizerTechnology";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "66":
					url="/citrusmonitor/technology.do?method=findMedicineTechnology";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "77":
					url="/citrusmonitor/law.do?method=frontLaw";
					
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "88":
					url="/citrusmonitor/question.do?method=showQ";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				
				case "99":
					url="/citrusmonitor/announcement.do?method=showAnnounce";
					message=distribution(request,response,url,currentPage,pageSize);
					break;	
				case "101":
					url="/citrusmonitor/technology.do?method=findPestTechnology";
					message=distribution(request,response,url,currentPage,pageSize);
					break;
				case "102":
					url="/citrusmonitor/technology.do?method=findDiseaseTechnology";
					message=distribution(request,response,url,currentPage,pageSize);
					break;	
					
			}
			
		}
		
		
		
		return null;
	}
	/**
	 * 为了message的转发，需要ci和id(具体查看某一条)
	 * @param request
	 * @param response
	 * @return
	 */
	
	public String branch(HttpServletRequest request,HttpServletResponse response) {
		String ci=request.getParameter("ci");
		String id=request.getParameter("id");
		String url="";
		String message="";
		if(id==null||id.equals("")) {
			return JSON.toJSONString(new Message("2","id不能为空"));
		}
		if(ci==null||ci.equals(""))
			return JSON.toJSONString(new Message("2","ci不能为空"));
		else {
			switch(ci) {
				case "11":
					url="/citrusmonitor/disease.do?method=findDiseaseById";
					message=detail(request,response,url,id);
					break;
				case "22":
					url="/citrusmonitor/pest.do?method=findPestById";
					message=detail(request,response,url,id);
					break;
				case "33":
					url="/citrusmonitor/citrus.do?method=getCitrusById";
					message=detail(request,response,url,id);
					break;
				case "44":
					url="/citrusmonitor/technology.do?method=getTechnologyById";
					message=detail(request,response,url,id);
					break;
				case "55":
					url="/citrusmonitor/technology.do?method=getTechnologyById";
					message=detail(request,response,url,id);
					break;
				case "66":
				case "101"://pest
				case "102"://disease
					url="/citrusmonitor/technology.do?method=getTechnologyById";
					message=detail(request,response,url,id);
					break;
				case "77":
					url="/citrusmonitor/law.do?method=detail";
					message=detail(request,response,url,id);
					break;
				case "88":
					url="";
					message=detail(request,response,url,id);
					break;
				case "99":
					url="/citrusmonitor/announcement.do?method=showAnById";
					message=detail(request,response,url,id);
					break;			
					
			}
			
		}
		
		return null;
	}
	/**
	 * 详细信息转发
	 * @param request
	 * @param response
	 * @param url
	 * @return
	 */
	private String detail(HttpServletRequest request,HttpServletResponse response,String url,String id) {
		String resPath=url+"&id="+id;
		try {
			response.sendRedirect(resPath);
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return JSON.toJSONString(new Message("2","服务器出错"));
		}
		
	}
	
	/**
	 * 分页转发
	 * @param request
	 * @param response
	 * @param url
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	private String distribution(HttpServletRequest request,HttpServletResponse response,String url,String currentPage,String pageSize){
		String reqUrl=url;
		if(currentPage!=null&&!currentPage.equals("")) {
			reqUrl+="&currentPage=";
			reqUrl+=currentPage;
		}
		if(pageSize!=null&&!pageSize.equals("")) {
			reqUrl+="&pageSize=";
			reqUrl+=pageSize;
			
		}
		try {
			//request.getRequestDispatcher(reqUrl).forward(request, response);
			response.sendRedirect(reqUrl);
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return JSON.toJSONString(new Message("2","服务器出错"));
			
		}
		
	}
}
